package com.dk.controller;


import com.alibaba.fastjson.JSONObject;
import com.dk.api.IntelligentFeignApi;
import com.dk.constant.CollectionConstant;
import com.dk.enums.ResultEnum;
import com.dk.pojo.TbCollection;
import com.dk.result.Result;
import com.dk.service.DicFunctionService;
import com.dk.service.DicRecordService;
import com.dk.service.TbCollectionService;
import com.dk.util.JwtUtil;
import com.dk.vo.DicFunctionVO;
import com.dk.vo.DicRecordVO;
import com.dk.vo.TbUserVO;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cars")
public class TbCarsController {

    @Autowired
    private DicRecordService recordService;
    @Autowired
    private DicFunctionService functionService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private TbCollectionService collectionService;
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    IntelligentFeignApi intelligentFeignApi;

    @GetMapping("getCarDicRecord")
    @ApiOperation(value = "获取车辆详情信息")
    @ApiImplicitParam(name = "cid", value = "车辆id", required = true, type = "query")
    public Result getCarDicRecord(@RequestParam Long cid, @RequestHeader("AuthToken") String token) {
        Claims claims = jwtUtil.parseJwt(token);
        TbUserVO user = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);
        intelligentFeignApi.getCarId(cid,token);

        List<DicRecordVO> recordVOList = null;
        try {
            recordVOList = recordService.getCarDicRecord(cid);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
        return Result.OK().setData(recordVOList);
    }

    @GetMapping("getCarDicFunction")
    @ApiOperation(value = "获取车辆功能信息")
    @ApiImplicitParam(name = "cid", value = "车辆id", required = true, type = "query")
    public Result getCarDicFunction(@RequestParam Long cid) {
        List<DicFunctionVO> functionVOList = null;
        try {
            functionVOList = functionService.getCarDicFunction(cid);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
        return Result.OK().setData(functionVOList);
    }

    @PostMapping("collect")
    @ApiOperation(value = "收藏")
    @ApiImplicitParam(name = "cid", value = "车辆id", required = true, type = "path")
    public Result collect(@RequestParam Long cid, @RequestHeader("AuthToken") String AuthToken) {

        Claims claims = jwtUtil.parseJwt(AuthToken);
        TbUserVO userVO = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);

        if (userVO == null) {
            return Result.respones(ResultEnum.LOGIN_AUTH); //请登录
        } else {
            Long uid = userVO.getUserid();
            //查询favorite表数据
            TbCollection collection = collectionService.selectAllByUidAndMid(uid, cid);
            if (collection == null) {
                //将收藏的数据存入mysql数据库
                collectionService.addCollection(cid, uid);

                //MQ --> 生产者
                rocketMQTemplate.convertAndSend(CollectionConstant.COLLECTION_CARS_TOPIC, CollectionConstant.COLLECTION_TAG);

                return Result.respones(ResultEnum.COLLECT_SUCCESS); //收藏成功
            } else {
                return Result.respones(ResultEnum.REPETITION_COLLECT); //用户已收藏,请勿重复收藏
            }
        }
    }

}
